package managers
{
	import events.DragEvent;
	
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import mx.controls.listClasses.ListBase;
	import mx.core.Container;
	import mx.core.UIComponent;
	
	import helpers.normal.ItemRender;

	public class DragManager
	{
		private var container:Container;
		
		public static var instance:DragManager;
		
		private var regspanel:Dictionary = new Dictionary();
		
		public function DragManager(container:Container)
		{
			this.container = container;
			instance = this;
		}
		
		public function register(control:UIComponent,target:UIComponent):void
		{
			if(regspanel[control]){
				remove(control);
			}
			regspanel[control] = target;
			control.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			control.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
		}
		
		private function mouseDownHandler(event:MouseEvent):void
		{
			var control:UIComponent = event.currentTarget as UIComponent;
			var target:UIComponent = regspanel[control];
			if(target){
				target.startDrag(false,new Rectangle(0,0,container.width-target.width,container.height-target.height));
			}
		}
		
		private function mouseUpHandler(event:MouseEvent):void
		{
			var control:UIComponent = event.currentTarget as UIComponent;
			var target:UIComponent = regspanel[control];
			if(target){
				target.stopDrag();
			}
		}
		
		public function remove(control:UIComponent):void
		{
			control.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			control.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
			delete regspanel[control];
		}
		
		
		//-------------------------
		// 拖动的方法
		//-------------------------
		private var offset_x:int;
		private var offset_y:int;
		
		private var _isDraging:Boolean = false;
		
		private var dragImg:Shape;
		
		private var source:ItemRender;
		private var dropTarget:ItemRender;
		
		public function doDrag(source:ItemRender,event:MouseEvent,img:Shape,x:Number=NaN,y:Number=NaN):void
		{
			isDraging = false;
			this.source = source;
			if(isNaN(x))
				offset_x = -event.localX;
			else
				offset_x = x;
			if(isNaN(y))
				offset_y = -event.localY;
			else
				offset_y = y;
			img.x = this.container.mouseX+offset_x;
			img.y = this.container.mouseY+offset_y;
			img.alpha = 0.5;
			dragImg = img;
			isDraging = true;
		}
		
		private function startDrag():void
		{
			this.container.stage.addChild(dragImg);
		}
		
		public function acceptDrag(target:ItemRender):void
		{
			this.dropTarget = target;
			this.dropTarget.addEventListener(MouseEvent.MOUSE_OUT,dropTargetMouseOutHandler);
		}
		
		private function dropTargetMouseOutHandler(event:MouseEvent):void
		{
			if(dragImg && this.dropTarget){
				this.dropTarget.removeEventListener(MouseEvent.MOUSE_OUT,dropTargetMouseOutHandler);
				this.dropTarget = null;				
			}
		}
		
		/**
		 * 拖放到……  
		 * @param event
		 * 
		 */
		public function onDrop(evt:MouseEvent):void
		{
			if(dragImg && dragImg.parent && isDraging){
				if(this.source&&this.source.owner){
					var event:DragEvent = new DragEvent(DragEvent.MY_DRAG_DROP,this.dropTarget,'move');
					this.source.owner.dispatchEvent(event);
					if(event.issOwnDo){//如果有自定义的话，就直接返回，后续的就不处理
						return;
					}
				}
				var fromobj:Object = this.source.data.data;
				var toobj:Object = this.dropTarget.data.data;
				if(toobj){
					this.source.data.empty = false;
				}
				else
				{
					this.source.data.empty = true;
				}
				this.source.data.data = toobj;
				if(fromobj){
					this.dropTarget.data.empty = false;
				}
				else
				{
					this.dropTarget.data.empty = true;
				}
				this.dropTarget.data.data = fromobj;
				if(this.source.owner is ListBase){
					(this.source.owner as ListBase).selectedIndex = -1;
				}
				isDraging = false;
			}
		}
		
		/**
		 * 结束拖放 
		 * 
		 */
		public function completeDrag():void
		{
			if(dragImg)
			{
				if(this.source&&this.source.owner){
					var event:DragEvent = new DragEvent(DragEvent.MY_DRAG_COMPLETE,this.dropTarget);
					this.source.owner.dispatchEvent(event);
				}
				isDraging = false;
			}
		}
		
		/**
		 * 拖动时候的鼠标跟随 
		 * @param event
		 * 
		 */
		private function containerMouseMoveHandler(event:MouseEvent):void
		{
			if(!dragImg.parent){
				startDrag();
			}
			dragImg.x = this.container.mouseX + offset_x;
			dragImg.y = this.container.mouseY + offset_y;
		}
		/**
		 * 没有任何 
		 * @param event
		 * 
		 */		
		private function containerMouseUpHandler(event:MouseEvent):void
		{
			if(isDraging){
				completeDrag();
			}
		}

		public function get isDraging():Boolean
		{
			return _isDraging;
		}

		/**
		 * 设置拖放状态，false时移除监听 
		 * @param value
		 * 
		 */
		public function set isDraging(value:Boolean):void
		{
			_isDraging = value;
			if(value)
			{
				this.container.stage.addEventListener(MouseEvent.MOUSE_MOVE,containerMouseMoveHandler);
				this.container.stage.addEventListener(MouseEvent.MOUSE_UP,containerMouseUpHandler);
			}
			else
			{
				if(dragImg && dragImg.parent)
					dragImg.parent.removeChild(dragImg);
				dragImg = null;
				source = null;
				dropTarget = null;
				this.container.stage.removeEventListener(MouseEvent.MOUSE_MOVE,containerMouseMoveHandler);
				this.container.stage.removeEventListener(MouseEvent.MOUSE_UP,containerMouseUpHandler);
			}
		}

		
	}
}